package com.filldream.fastboot.web.controller.common;

import com.filldream.fastboot.base.entity.Role;
import com.filldream.fastboot.base.entity.SmsTemplate;
import com.filldream.fastboot.base.mapper.RoleMapper;
import com.filldream.fastboot.common.annotation.DataSourceAnnotation;
import com.filldream.fastboot.common.annotation.RepeatRequestAnnotation;
import com.filldream.fastboot.common.exception.GlobalException;
import com.filldream.fastboot.common.entity.*;
import com.filldream.fastboot.common.util.SmsUtil;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.common.util.entity.sms.SmsTemplateModel;
import com.filldream.fastboot.core.LoginHolder;
import com.filldream.fastboot.core.cache.CacheMap;
import com.filldream.fastboot.core.cache.CacheUtil;
import com.filldream.fastboot.core.manager.SmsRecordManager;
import com.filldream.fastboot.core.mq.MqProducer;
import com.filldream.fastboot.core.mq.Mq_QUEUQ;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author RickSun && iFillDream
 * @Description 体统健康自测
 * @Date 2020/5/13 11:10
 * @Param
 * @return
 **/
@AllArgsConstructor
@Api(tags = "系统自测")
@RestController
@RequestMapping("/common/health")
public class HealthController {

    private RoleMapper roleMapper;

    private SmsRecordManager smsRecordManager;

    private ApplicationContext applicationContext;

    private MqProducer mqProducer;

    public static void main(String[] args) {

    }

    @PostMapping("/test")
    @ApiOperation(value = "测试Controller")
    public R test(){
        return R.successd(LoginHolder.getUserIdOrNull());
    }

    @PostMapping("/testMq")
    @ApiOperation(value = "测试Mq")
    public void testMq(){
        mqProducer.delaySend(Mq_QUEUQ.TAG,"111",2000L);
    }

    @RepeatRequestAnnotation(overTime = 10)
    @PostMapping("/testMsg")
    @ApiOperation(value = "测试短信")
    public R testMsg(String cellPhone, String password){
        check(password);
        Map<String,String> smsMap = new HashMap<>();
        smsMap.put("code","111111");
        SmsTemplate byType = smsRecordManager.getByType(1);
        SmsTemplateModel smsTemplateModel = TransferUtil.changeToObj(byType, SmsTemplateModel.class);
        smsTemplateModel.setCellPhone( cellPhone );
        smsTemplateModel.setMessageMap( smsMap );
        Map<String, Object> map = SmsUtil.sendNormalMsg(smsTemplateModel);
        return R.autoSuccess(map);
    }

    @RepeatRequestAnnotation(overTime = 10)
    @PostMapping("/testDB")
    @ApiOperation(value = "测试数据源1")
    public R<Role> testDB(){
        return R.autoSuccess(roleMapper.selectById(1L));
    }

    @DataSourceAnnotation(dbName = "db2")
    @RepeatRequestAnnotation(overTime = 10)
    @PostMapping("/testDB2")
    @ApiOperation(value = "测试数据源2")
    public R<?> testDB2(@RequestBody BasePage basePage){
        return R.fail("请确保配置好数据源后再编写测试代码");
    }

    @RepeatRequestAnnotation(overTime = 5)
    @PostMapping("/errorCodeList")
    @ApiOperation(value = "错误码查询")
    public R<List<String>> errorCodeList(Integer code){
        ErrorCode[] alarmGrades = ErrorCode.values();
        List<String> resultList = new ArrayList<>();
        for (ErrorCode alarmGrade : alarmGrades) {
            if( code != null ){
                if( alarmGrade.getCode() == code  ){
                    resultList.add(alarmGrade.getString());
                    return R.autoSuccess(resultList);
                }
            }else {
                resultList.add(alarmGrade.getString());
            }
        }
        return R.autoSuccess(resultList).put("size",resultList.size());
    }

    @RepeatRequestAnnotation(overTime = 60)
    @PostMapping("/testNull")
    @ApiOperation(value = "测试钉钉是否正常工作")
    public B sendCode(){
        Integer i = null;
        return B.checkSuccess(i > 0);
    }

    @RepeatRequestAnnotation(overTime = 3)
    @PostMapping("/batchDelCache")
    @ApiOperation(value = "批量删除缓存",notes = "key缓存键，password接口安全密码")
    public B batchDelCache(String key, String password){
        check2(password);
        return B.checkSuccess(CacheUtil.delKey(key));
    }

    @RepeatRequestAnnotation(overTime = 3)
    @PostMapping("/delCache")
    @ApiOperation(value = "删除缓存",notes = "key缓存键，password接口安全密码")
    public B delCache(String key, String password){
        check(password);
        return B.checkSuccess( CacheUtil.delKey(key) );
    }

    @RepeatRequestAnnotation
    @PostMapping("/getCache")
    @ApiOperation(value = "获取缓存",notes = "key缓存键，password接口安全密码")
    public R getCache(String key, String password){
        check(password);
        return R.autoSuccess(CacheUtil.getStr(key));
    }

    @RepeatRequestAnnotation
    @PostMapping("/addCache")
    @ApiOperation(value = "添加缓存",notes = "key缓存键，password接口安全密码")
    public R addCache(String password){
        check(password);
        String keyName = CacheMap.getKey(CacheMap.MALL_CATEGORY_DETAIL,0);
        return R.successd(CacheUtil.cacheObj(keyName,5));
    }

    private static boolean check(String password){
        if(StringUtils.isBlank(password) || !password.equals("111222")){
            throw new GlobalException(ErrorCode.AUTH_ERR.reMsg());
        }
        return true;
    }

    private static boolean check2(String password){
        if(StringUtils.isBlank(password) || !password.equals("111222111222")){
            throw new GlobalException(ErrorCode.AUTH_ERR.reMsg());
        }
        return true;
    }



}
